<template>
  <div>
    <div>
      <FormPanel ref="formPanel" :list="formList" :cols="2" :labelWidth="110" :isSubmitBtn="false" />
    </div>
    <div class="dialogBottmView">
      <!--<multiuse-button type="primary" class="btnStyle" :click="closeMenu.bind(this,1)">审核通过</multiuse-button>-->
      <multiuse-button type="primary" class="btnStyle" :click="closeMenu.bind(this, 1)">保存</multiuse-button>
      <multiuse-button type="primary" class="btnStyle" :click="closeMenu.bind(this, 2)">关闭</multiuse-button>
    </div>
  </div>
</template>

<script>
import { downFile, addVehicleWarranty, getWarrantyRule, getVehicle, addVehicleWarrantyRule } from '@/api/repair/newEnergyVehicleLifetime/vehicleLifetimeReviewCDuan';

import { dealerMixin } from '@/pages/repair/batchEventManagement/dealerMixin';
import { dictionary } from '@/utils/dictMixin';
import { isEmpty } from '@/utils/utils';
import { exportFile } from '@/utils/index';
import { getAppId } from '@/assets/js/auth';

export default {
  name: 'detail',
  mixins: [dictionary, dealerMixin],
  props: ['handleType', 'warrantyRuleId'],
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList(),
      visibleVin: false,
      fileObj: {},
      dataSource: []
    };
  },
  components: {},
  mounted() {
    if (getAppId() == 'arcfox') {
      this.formList = this.createFormListArcfox();
    } else {
      this.formList = this.createFormList();
    }
    if (this.handleType == 'edit') {
      this.formList.find(x => x.fieldName == 'warrantyType').disabled = true;
      this.formList.find(x => x.fieldName == 'isFirstOwner').disabled = true;
    }

    this.getData();
  },
  methods: {
    changeHandle(val) {
      this.saveData = val;
    },
    closeDialog() {
      this.visibleVin = false;
    },

    /*根据ID查询*/
    async getData() {
      if (this.warrantyRuleId == null || this.warrantyRuleId == '' || !this.warrantyRuleId) {
        return;
      }
      let res = await getWarrantyRule({ warrantyRuleId: this.warrantyRuleId });

      if (res.resultCode == 200 && res.success == true) {
        this.formList.find(x => {
          x.initialValue = res.data[x.fieldName];
        });
      }
    },

    async saveOrSubmit(type) {
      let urlObj = this.formList.find(item => item.fieldName == 'url').initialValue[0];
      if (isEmpty(urlObj)) {
        urlObj = {};
      }
      let path = isEmpty(urlObj.url) ? '' : urlObj.url;
      let fileName = isEmpty(urlObj.name) ? '' : urlObj.name;
      this.$refs.formPanel.SUBMIT_FORM();
      let obj = this.saveData;
      obj.driveLicenseImg = path;
      obj.driveLicenseName = fileName;
      /*10221003 保存  提交 10221001*/
      if (this.handleType == 'add') {
        obj.warrantyRuleId = '';
      } else {
        obj.warrantyRuleId = this.warrantyRuleId;
      }
      if (1 == type) {
        //保存
        obj.status = '10221003';
      } else {
        //提交
        obj.status = '10221001';
      }

      let res = await addVehicleWarranty(obj);

      if (res.resultCode == 200 && res.success == true) {
        this.$notify.success({ title: '提示信息', message: '操作成功' });

        this.$emit('close', 'refresh');
      }
    },

    async closeMenu(type) {
      if (1 == type) {
        let params = this.$refs.formPanel.SUBMIT_FORM();
        params.warrantyRuleId = this.warrantyRuleId;
        let res = await addVehicleWarrantyRule(params);

        if (res.resultCode == 200 && res.success == true) {
          this.$notify.success({ title: '提示信息', message: '操作成功' });

          this.$emit('close', 'refresh');
        }
      } else {
        this.$emit('close');
      }
    },

    downloadFun(row) {
      this.$confirm('是否文件保存至本地文件夹?', '提示', {
        confirmButtonText: '下载',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          downFile({ filePath: row['path'] })
            .then(res => {
              console.log(res, 'res');
              exportFile(res.data, row['fileName'], '');
            })
            .catch(err => {});
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
    },

    createFormList() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '车辆信息'
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          disabled: true,
          placeholder: '',
          readonly: false,
          rules: [{ required: true, trigger: 'change', message: '请选择vin' }],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '车牌号',
          fieldName: 'license',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          placeholder: '请输入',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '车型',
          fieldName: 'model',
          placeholder: '',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '配置名称',
          fieldName: 'apackage',
          placeholder: '请输入',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '内饰',
          fieldName: 'innerColor',
          placeholder: '请选择',
          disabled: true,
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '外色',
          fieldName: 'color',
          placeholder: '请选择',
          disabled: true,
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '整车编码',
          fieldName: 'counts35555',
          placeholder: '请选择',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '电机号',
          fieldName: 'motorNo',
          placeholder: '请选择',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '当前行驶里程',
          fieldName: 'currentMileage',
          placeholder: '请选择',
          labelWidth: '110px',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: ''
        },

        {
          type: 'BREAK_SPACE',
          label: '客户信息'
        },

        {
          type: 'INPUT',
          label: '车主姓名',
          fieldName: 'ownerName',
          disabled: true,
          placeholder: '',
          // rules: [{required: true, trigger: 'change'}],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '车主电话',
          fieldName: 'mobile',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          placeholder: '请输入',
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '证件类型',
          fieldName: 'certType',
          placeholder: '',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: '',
          itemList: this.createDictList(8013)
        },
        {
          type: 'INPUT',
          label: '证件号码',
          fieldName: 'certNo',
          placeholder: '请输入',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '购车用途',
          fieldName: 'vehiclePurpose',
          placeholder: '请选择',
          disabled: true,
          initialValue: '',
          itemList: this.createDictList('1511')
        },
        {
          type: 'DATE',
          label: '保修起始日期',
          fieldName: 'salesDate',
          placeholder: '请选择',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '联系地址',
          fieldName: 'address',
          placeholder: '请选择',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: ''
        },
        {
          type: 'DATE',
          label: '行驶证注册日期',
          fieldName: 'driveLicenseDate',
          format: 'yyyy-MM-dd',
          placeholder: '请选择',
          disabled: true,
          labelWidth: '110px',
          rules: [{ required: true, trigger: 'change', message: '请选择行驶证注册日期' }],
          initialValue: ''
        },
        {
          type: 'UPLOAD_FILE',
          label: '上传',
          fieldName: 'url',
          upload: {
            actionUrl: '/api/file/oss/upload', //上传的地址
            fileTypes: ['jpg', 'png', 'pdf', 'rar'],
            max: 1024 * 100,
            limit: 1
          },
          initialValue: '',
          disabled: true,
          readonly: true,
          selfCols: 1,
          rules: [{ required: false, message: '请上传文件', trigger: 'blur' }]
        }
      ];
    },
    createFormListArcfox() {
      return [
        {
          type: 'SELECT',
          label: '质保类型',
          fieldName: 'warrantyType',
          placeholder: '请选择',
          rules: [{ required: true, trigger: 'change', message: '请选择质保类型' }],
          itemList: this.createDictList('8888'),
          disabled: false
        },
        {
          type: 'SELECT',
          label: '是否首任车主',
          fieldName: 'isFirstOwner',
          placeholder: '请选择',
          rules: [{ required: true, trigger: 'change', message: '请选择是否首任车主' }],
          itemList: this.createDictList('1004'),
          disabled: false
        },
        {
          type: 'INPUT_NUMBER',
          label: '质保里程(km)',
          fieldName: 'warrantyMileage',
          placeholder: '请选择',
          rules: [{ required: true, trigger: 'change', message: '请输入质保里程' }],
          min: 1
        },
        {
          type: 'INPUT_NUMBER',
          label: '质保时间(年)',
          fieldName: 'warrantyTime',
          placeholder: '请选择',
          rules: [
            { required: true, trigger: 'change', message: '请输入质保时间' },
            { pattern: /^([1-9]|[1-9]\\d|100)$/, trigger: 'change', message: '请输入1-100之间的正整数' }
          ],
          min: 1
        }
      ];
    }
  }
};
</script>

<style scoped lang="less">
.bg1 {
  border: 1px solid @borderColor;
  border-radius: @borderRadius;
  box-shadow: @boxShadow;
  padding: @modulePadding;
  padding-left: 0;
  padding-top: 15px;
  padding-bottom: 15px;
}

.minBtnStytle {
  text-align: center;
  background: #00feff;
  padding-left: 10px;
}

.bg2 {
  margin-top: 10px;
  margin-bottom: 10px;
}

.btn {
  text-align: center;
}

.btnStyle {
  margin-top: 10px;
}
</style>
